@extends('core::layouts.master')

@section('content')
    <table class="layui-hide" id="data_spu_table" lay-filter="data_spu_table"></table>

    <x-wd-formItem type="submit">
        <a class="layui-btn" id="j_picker_submit">提交</a>
    </x-wd-formItem>
@endsection

@push('script')
    <script type="text/html" id="tpl_title">
        @{{# if(d.out_douyin_product_id){ }}
        <a class="layui-badge mr-1 bg-gray-900" title="抖音商品ID：@{{d.out_douyin_product_id}}">抖音</a>
        @{{# } }}
        <span>@{{ d.name }}</span>
    </script>
    <script type="text/html" id="game_status">
        <input type="checkbox" name="switch" lay-skin="switch" lay-text="上架|下架" lay-filter="updown" value="@{{ d.id }}"
            @{{# if(d.status=='up'){ }}
             checked
            @{{# } }}
        >
    </script>
    <script type="text/html" id="product_thumb">
        <img src="@{{ d.thumb || "/assets/empty.jpg" }}" alt="" height="65" class="block cursor-pointer">
    </script>
    <script type="text/html" id="tpl_actions">
        <div>
            <a lay-event="preview" class="mr-1">预览</a>
        </div>
    </script>

    <script>
        layui.use(['table', 'element', 'form'], function(){
            var table = layui.table,
                admin = parent.layui.admin,
                form = layui.form,
                element = layui.element,
                url = {
                    preview: '{{ route('shop.preview') }}'
                };

            table.render({
                elem: '#data_spu_table',
                url: '{!! URL::full() !!}',
                autoShow: '{{ route("shop.spu.show", "_id_") }}',
                where: {'orderBy': 'created_at', 'sortedBy': 'desc'},
                action: [{'text': '推送到抖音小店', 'event': 'push_to_douyin'}],
                page: true,
                canSearch: true,
                toolbar: 'default',
                height: 'full-110',
                lineHeight: 65,
                cellMinWidth: 80,
                done: tableDone,
                cols: [[
                    {"type":"checkbox","fixed":"left"},
                    {"field":"uid","title":"商品ID","width":150},
                    {"field":"thumb","title":"缩略图","templet":"#product_thumb","width":100},
                    {"field":"name","title":"名称","width":300, "templet": "#tpl_title"},
                    {"field":"pay_type_label","title": "支付方式"},
                    {"field":"category_id_label","title": "类别"},
                    {"field":"count","title":"总库存","templet":'<div>@{{d.count.store}}</div>'},
                    {"field":"count","title":"销量","templet":'<div>@{{d.count.sale}}</div>'},
                    {"field":"status","title":"状态",templet: "#game_status",width:95},
                    {"field":"created_at","title":"创建时间"},
                    {"field":"uid","title":"操作","templet":"#tpl_actions"},
                ]]
            });

            //监听行工具事件
            table.on('toolbar(data_spu_table)', function(obj){
                var checked = table.checkStatus('data_spu_table');

                if(obj.event == 'create') {
                    var createModal = admin.openTab('{{ route("shop.spu.create", \request()->all()) }}', '新增数据')
                    return true;
                }

                if((obj.event == 'delete' || obj.event == 'update' || obj.event == "push_to_douyin") && checked.data.length >0 ) {
                    if(checked.data.length !== 1) {
                        layer.msg('请选择一条数据!');
                        return false;
                    }

                    if(obj.event === 'delete') {
                        layer.confirm('真的删除行么', function(index){
                            layui.util.request.post('{{ route("shop.spu.destroy", "_id_") }}'.replace('_id_', checked.data[0].id), {'_method': 'DELETE'}, function(res){
                                layer.msg('删除成功');
                                table.reload('data_spu_table')
                            })

                            layer.close(index);
                        });
                    }else if(obj.event === 'update') {
                        var createModal = admin.openTab(
                            '{{route("shop.spu.edit", "_id_")}}'.replace('_id_', checked.data[0].id)+'?type='+checked.data[0].type,
                            '修改数据#'+checked.data[0].id)
                        return true;
                    }else if(obj.event === "push_to_douyin") {
                        if(checked.data[0].out_douyin_product_id) {
                            layer.msg('该商品已经推送到抖音');
                            return false;
                        }
                        var createModal = layer.open({
                            type: 2,
                            area: ['800px','80%'],
                            title: '推送商品到抖音',
                            content: "/open-shop/douyin/push?product_id="+checked.data[0].uid
                        })
                        return true;
                    }
                }
            });

            table.on('tool(data_spu_table)', function(obj){
                parent.layer.open({
                    type: 2,
                    area: ['500px', '90%'],
                    content: url.preview + '?uid=' + obj.data.uid
                })
            });

            // 上下线
            function tableDone() {
                form.on('switch(updown)', function(e){
                    var _status = e.elem.checked?'up':'down';
                    // layui.util.request.post("".replace('_id_', e.value), {'status': _status}, function(res) {
                    //     layer.msg('已'+(_status=='up'?'上线':'下线'))
                    // })
                })

                //图片预览
                layer.photos({
                    photos: "#lumina_app"
                })
            }

            // 选品
            $("#j_picker_submit").click(function(){
                var checked = table.checkStatus('data_spu_table'),
                    ids = [];

                checked.data.map(function(n, i){
                    ids.push(n.uid)
                })

                if(ids.length > 0) {
                    layui.util.request.post('{{ route("shop.spu.pick") }}', {'ids': ids.join(',')}, function(res) {
                        parent.layer.msg('选品成功,请刷新列表');
                        if(parent.layer && window.name) {
                            parent.layer.close(parent.layer.getFrameIndex(window.name));
                        }
                    })
                }else{
                    layer.msg('请选择商品')
                    return false;
                }
            })
        });
    </script>
@endpush
